<?php
class Default_Model_Location extends Zend_Db_Table{

	protected $_name = LOCATION_TABLE;
	protected $_primary = 'location_id';

	private $LOCATION_ID = 'location_id';
	private $CITY_ID = 'city_id';
	private $USER_ID = "user_id";
	private $NAME = "name";
	private $LOCATION_CREATED_TIME = 'created_time';

	protected $db;

	public function __construct(){
		//Goi ket noi voi Zend Db
		$this->db = Zend_Registry::get('connectDB');
	}

	protected $_messageError = null;

	public function countLocation(){
		$select = $this->db->select()
		->from(LOCATION_TABLE,array('COUNT(location_id) AS totalLocation'));
		$result = $this->db->fetchOne($select);
		return $result;
	}

	public function listAllLocation(){
		$select = $this->db->select()->from(LOCATION_TABLE);
		$select->order($this->LOCATION_ID.' DESC');
		return $select;
	}
	
	public function listAllLocationNonPaging(){
		$select = $this->db->select()->from(LOCATION_TABLE);
		$select->order($this->LOCATION_ID.' DESC');
		$result  = $this->db->fetchAll($select);
		return $result;
	}

	public function getLocationInfoById($locId) {
		$select = $this->db->select()->from(LOCATION_TABLE)->where($this->LOCATION_ID.' = ?',$locId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	public function getLocationByUserId($userId) {
		$select = $this->db->select()->from(LOCATION_TABLE)->where($this->USER_ID.' = ?',$userId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	public function getLocationByCity($cityId) {
		$select = $this->db->select()->from(LOCATION_TABLE)->where($this->CITY_ID.' = ?',$cityId);
		//$result  = $this->db->fetchAll($select);
		return $select;
	}
	
	// get last location
	public function getLastLocation(){
		$select = $this->db->select()->from(LOCATION_TABLE);
		$select->order($this->LOCATION_ID.' DESC')->limit(1);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	// insert location
	public function insertLocation($arrParam = null){
		try {
			$this->db->insert(LOCATION_TABLE,$arrParam);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}
	
	// update info
	public function updateLocationInfo($arrParam = null, $locId){
		try {
			$this->db->update(LOCATION_TABLE, $arrParam, $this->LOCATION_ID.' = '.$locId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}

	// Xoa thong tin location
	public function deleteLocationById($locId){
		try {
			$this->db->delete(LOCATION_TABLE,$this->LOCATION_ID.' = ' . $locId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}
	
	public function deleteLocationByUserId($userId){
		try {
			$this->db->delete(LOCATION_TABLE,$this->USER_ID.' = ' . $userId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}
	
	// get location statistic
	public function locationStatisticByMonth($month) {
		try {
			$select = $this->db->select()->from(LOCATION_TABLE)->where($this->LOCATION_CREATED_TIME.' LIKE "%'.$month.'%"');
			$result  = $this->db->fetchAll($select);
			return $result;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
		}
	}

	public function getError(){
		return $this->_messageError;
	}
}